<?php

namespace app\admin\controller\depot;

use app\common\controller\Backend;

class Supplier extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Depot.Supplier");
        $this->RegionModel = model('Region');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['provinces', 'citys', 'districts'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->model
                ->with(['provinces', 'citys', 'districts'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo   $this->SubjectOrderModel->getlastSql();exit;
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    // 添加
    public function add()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $data = [
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
            ];

            if ($params['region']) {
                $region = explode('/', $params['region']);

                if (count($region) < 3) {
                    $this->error('请重新选择省市区!');
                }
                $pathcode = $this->RegionModel->where('name', $region[2])->value('parentpath');

                $pathcode = explode(',', $pathcode);

                list($province, $city, $district) = $pathcode;
                $data['province'] = $province;
                $data['city'] = $city;
                $data['district'] = $district;
            }
            $result = $this->model->validate('commom/Depot/Supplier')->save($data);
            if ($result === FALSE) {
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('No results were found'));
            exit;
        }
        // 省份
        $province = $this->RegionModel->where('code', $row['province'])->value('name');
        // 市
        $city = $this->RegionModel->where('code', $row['city'])->value('name');
        // 区
        $district = $this->RegionModel->where('code', $row['district'])->value('name');

        $region = [
            'province' => $row['province'],
            'city' => $row['city'],
            'district' => $row['district']
        ];
        $region = $province . '/' . $city . '/' . $district;
        $this->assign([
            'row' => $row,
            'region' => $region
        ]);
        $this->assign([
            'row' => $row,
            'region' => $region
        ]);
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $data = [
                'id' => $ids,
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
            ];

            if ($params['region']) {
                $region = explode('/', $params['region']);

                if (count($region) < 3) {
                    $this->error('请重新选择省市区!');
                }
                $pathcode = $this->RegionModel->where('name', $region[2])->value('parentpath');

                $pathcode = explode(',', $pathcode);

                list($province, $city, $district) = $pathcode;
                $data['province'] = $province;
                $data['city'] = $city;
                $data['district'] = $district;
            }
            $result = $this->model->validate('commom/Depot/Supplier')->isUpdate(true)->save($data);
            if ($result === FALSE) {
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $ids = empty($ids) ? [] : explode(',', $ids);
        $row = $this->model->column('id');
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('No results were found'));
                exit;
            }
        }
        
        $result=$this->model->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $this->success();
            exit;
        }

    }
}